import 'package:dio/dio.dart';

class HttpClient {
  static final HttpClient _instance = HttpClient._internal();
  factory HttpClient(){
    return _instance;
  }

   Dio dio = new Dio();

  HttpClient._internal() {
    BaseOptions options = BaseOptions(
      baseUrl: "https://jsonplaceholder.typicode.com/", // 你的基础 URL
      connectTimeout: const Duration(seconds: 10),
      receiveTimeout: const Duration(seconds: 10),
      headers: {
        "Content-Type": "application/json",
      },
    );

    dio = Dio(options);

    // 添加日志拦截器（调试用）
    dio.interceptors.add(LogInterceptor(
      request: true,
      requestHeader: true,
      requestBody: true,
      responseHeader: false,
      responseBody: true,
    ));
  }

  /// GET 请求
  Future<Response> get(String path, {Map<String, dynamic>? params})  {
    return  dio.get(path, queryParameters: params);
  }

  /// POST 请求
  Future<Response> post(String path, {dynamic data})  {
    return  dio.post(path, data: data);
  }

  /// PUT 请求
  Future<Response> put(String path, {dynamic data})  {
    return  dio.put(path, data: data);
  }

  /// DELETE 请求
  Future<Response> delete(String path, {dynamic data})  {
    return  dio.delete(path, data: data);
  }
}
